package com.dog.basics.netty.agreement;

import com.dog.basics.common.util.ByteUtil;
import com.dog.basics.netty.consts.Const;
import com.dog.basics.netty.protobuf.dto.DogNewsContentDto;
import com.google.protobuf.MessageLite;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.buffer.UnpooledHeapByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

import static io.netty.buffer.Unpooled.wrappedBuffer;

/**
 * @program: d_tea_machine
 * @description: PROTOBUF . 编码器
 * @author: Guo
 * @create: 2020-01-04 16:25
 **/
public class DogProtobufEncoder extends MessageToByteEncoder<ByteBuf> {
    /**
     * 编码
     * @param ctx
     * @param msg
     * @param out`
     * @throws Exception
     */
    @Override
    protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
        //判断发送消息
        if(msg == null){
            throw new Exception("msg is null");
        }
        /**
         *起始符
         */
        out.writeBytes(Const.headDefaultSrt.getBytes());
        /**
         *帧长度
         */
        int dataLength = msg.readableBytes() ;

        int allLength = dataLength + Const.headFieldLength + Const.msgFieldLength + Const.versionAddressLength+ Const.tailFieldLength;
        out.writeBytes(ByteUtil.intToByteArray(allLength));
        /**
         *板号
         */
        out.writeByte(0);
        /**
         *指令字
         */
        //out.writeByte(msg.getType());

        // 数据域
        out.writeBytes(msg);
        // 校验
        int xor = 0 ;
        for (int i = 0; i < out.readableBytes(); i++) {
            xor ^= out.getByte(i);
        }
        out.writeByte(xor);
    }


    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {

     ByteBuf msg = new UnpooledHeapByteBuf(UnpooledByteBufAllocator.DEFAULT,2,255);

        //登录
        DogNewsContentDto.Message build = DogNewsContentDto.Message.newBuilder()
                .setMsgType(0)
                .setDevCode("1325625283005923329")
                .build();

        System.out.println(ByteUtil.bytesToHexString(build.toByteArray()));
        msg.writeBytes((build.toByteArray()));
        ByteBuf out = new UnpooledHeapByteBuf(UnpooledByteBufAllocator.DEFAULT,4,255);

        System.out.println(ByteUtil.bytesToHexString(wrappedBuffer(((MessageLite) build).toByteArray()).array() ));
        System.out.println(ByteUtil.bytesToHexString(msg.array()));
        /**
         *起始符
         */
        out.writeBytes(Const.headDefaultSrt.getBytes());
        /**
         *帧长度
         */
        int dataLength = msg.readableBytes() ;

        int allLength = dataLength + Const.headFieldLength + Const.msgFieldLength + Const.versionAddressLength+ Const.tailFieldLength;
        System.out.println(allLength);
        out.writeBytes(ByteUtil.intToByteArray(allLength));
        /**
         *板号
         */
        out.writeByte(0);
        /**
         *指令字
         */
        //out.writeByte(msg.getType());

        /**
         *数据域
         */
        out.writeBytes(msg);
        /**
         *校验
         */
        int xor = 0 ;
        for (int i = 0; i < out.readableBytes(); i++) {
            xor ^= out.getByte(i);
        }
        out.writeByte(xor);
        System.out.println(out.readableBytes());
        System.out.println(ByteUtil.bytesToHexString(out.array()));

    }
}
